package com.example.dou2204.roomdb;

import android.app.Application;

import androidx.room.Room;

public class DataBaseUtils {
    private DataBaseUtils(){}
    private static DataBaseUtils dataBaseUtils = new DataBaseUtils();//恶汉模式
    public static DataBaseUtils getInstance(){
        return dataBaseUtils;
    }
    private AppDatabase appDatabase;
    private VideoDao videoDao;
    private MoneyDao moneyDao;
    //实例化
    public void init(Application application){
        appDatabase = Room.databaseBuilder(application,AppDatabase.class,"2204A.db")
                .allowMainThreadQueries()//允许在主线程中执行
                .build();
        videoDao = appDatabase.getVideo();
        moneyDao = appDatabase.getMoneyDao();
    }
    //防止当内存不足时，videodao被回收，会造成后续的空指针异常的错误
    public VideoDao getVideoDao(){
        if(videoDao==null){
            videoDao = appDatabase.getVideo();
        }
        return videoDao;
    }
    public MoneyDao getMoneyDao(){
        if(moneyDao==null){
            moneyDao = appDatabase.getMoneyDao();
        }
        return moneyDao;
    }
}
